import { ref, getCurrentInstance } from "vue"
// utils
import { dataTypes } from "@/utils"
export function useModalEvent({ props }){
    // 解构
    const { emit } = getCurrentInstance()
    // ref
    const isLoading = ref(false)
    const visible = ref(false)
    const isInnerLoading = ref(false)
    /** 取消 */
    const useCancel = () => {
        visible.value = false
        emit("update:open", false)
    }

    /** 确定按钮 */
    const useOk = () => {
        const isFunction = dataTypes().isFunction(props.okAsync)
        if(!isFunction) {
            return
        }
        // 执行逻辑
        isLoading.value = true
        props.okAsync().then(() => {
            isLoading.value = false
            useCancel()
            emit("ok")
        }).catch(() => {
            isLoading.value = false
        })
    }

    /** 内容加载 */
    const useInnerLoading = () => {
        const isFunction = dataTypes().isFunction(props.innerLoading)
        if(!isFunction) {
            return
        }
        // 执行逻辑
        isInnerLoading.value = true
        props.innerLoading().then(() => {
            isInnerLoading.value = false
        }).catch(() => {
            isInnerLoading.value = false
        })
    }

    /** open callback */
    const useOpenCallback = () => {
        emit("open")
    }

    return {
        isLoading,
        isInnerLoading,
        visible,
        useOk,
        useCancel,
        useInnerLoading,
        useOpenCallback
    }
}